﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using DAL;
using System.Configuration;

namespace PeisongWeb.admin
{
    public partial class inspectorChargeCompany : adminPage
    {
        protected int sysAdminId
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["sysAdminId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["sysAdminId"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!IsPostBack)
                {
                    if (hasPermission("AJ", 1))
                    {
                        rpt_responsibleBind();
                    }
                    else
                    {
                        Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                        return;
                    }

                }
            }
        }

        protected void rpt_responsibleBind()
        {
            int departmentId = Convert.ToInt32(ConfigurationManager.AppSettings["departmentId"]);
            DataTable dt = logic.sysAdmin.getBydepartmentId(departmentId);
            rpt_responsible.DataSource = dt;
            rpt_responsible.DataBind();
        }

        protected void rpt_responsible_ItemCommand(object sourse, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                sysAdminId = Convert.ToInt32(e.CommandArgument.ToString());
                AspNetPager1.CurrentPageIndex = 1;
                rpt_icclistBind();
            }
            else if (e.CommandName == "Add")
            {
                sysAdminId = Convert.ToInt32(e.CommandArgument.ToString());
                rptBind();
                AspNetPager2.CurrentPageIndex = 1;
                add_buyer.Show();
            }
        }

        protected void rpt_icclistBind()
        {
            StringBuilder where = new StringBuilder();
            if (sysAdminId!=0)
            {
                where.AppendFormat("adminId={0}", sysAdminId);
            }
            string userName = logic.sysAdmin.getById(sysAdminId).Username;
            DataSet ds = logic.sysAdmin.selectInseptor(where.ToString(), "companyId desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            if (ds.Tables[1].Rows.Count!=0)
            {
                admin_list_content.Visible = true;
                sp_msg.Style["color"] = "#000000";
                sp_msg.InnerText = "现场检验员 - 【"+userName+"】所负责的客户：";
                AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);

                rpt_icclist.DataSource = ds.Tables[1];
                rpt_icclist.DataBind();
            }
            else
            {
                DataSet ds2 = logic.sysAdmin.selectInseptor(where.ToString(), "companyId desc", AspNetPager1.PageSize, 0);
                if (ds2.Tables[1].Rows.Count!=0)
                {
                    AspNetPager1.RecordCount = Convert.ToInt32(ds2.Tables[0].Rows[0][0]);
                    rpt_icclist.DataSource = ds2.Tables[1];
                    rpt_icclist.DataBind();
                }
                else
                {
                    sp_msg.Style["color"] = "red";
                    sp_msg.InnerText = "对不起，现场检验员【" + userName + "】没有设置负责客户！";
                    admin_list_content.Visible = false;
                }
            }
        }
        protected void rpt_icclist_ItemCommand(object sourse, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                int companyid = Convert.ToInt32(e.CommandArgument.ToString());
                InspectorChargeCompanyEntity iccEnt = new InspectorChargeCompanyEntity();
                iccEnt.adminId = sysAdminId;
                iccEnt.companyId = companyid;
                logic.inspectorChargeCompany.delete(iccEnt);
                rpt_icclistBind();
                Library.Script.ClientMsg("删除成功.");

            }
        }
        protected void btnAddCompany_Click(object sender, EventArgs e)
        {
 
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rpt_icclistBind();
        }
        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            rptBind();
            add_buyer.Show();
        }
        private void rptBind()
        {
            lbl_sysAdmin.Text = "现场检验员："+logic.sysAdmin.getById(sysAdminId).Username;
            StringBuilder where = new StringBuilder();
            where.Append("1=1");
            if (!string.IsNullOrEmpty(txt_CompanyNo.Text))
            {
                where.AppendFormat(" and companyNo='{0}'", txt_CompanyNo.Text);
            }
            if (!string.IsNullOrEmpty(txt_CompanyName.Text))
            {
                where.AppendFormat(" and company like '%{0}%'", txt_CompanyName.Text);
            }
            DataSet ds = logic.sysAdmin.selectWithOutInseptor(where.ToString(), "companyId", AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            AspNetPager2.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            if (ds.Tables[1].Rows.Count == 0)
            {
                if (AspNetPager2.CurrentPageIndex == 0)
                {
                    rpt_list.DataSource = ds.Tables[1];
                    rpt_list.DataBind();
                }
                else
                {
                    DataSet ds2 = logic.sysAdmin.selectWithOutInseptor(where.ToString(), "companyId", AspNetPager2.PageSize, 0);
                    AspNetPager2.RecordCount = Convert.ToInt32(ds2.Tables[0].Rows[0][0]);
                    rpt_list.DataSource = ds2.Tables[1];
                    rpt_list.DataBind();
                }
            }
            else
            {
                rpt_list.DataSource = ds.Tables[1];
                rpt_list.DataBind();
            }
        }
        protected void lbn_search_Click(object sender, EventArgs e)
        {
            rptBind();
            txt_CompanyName.Text = "";
            txt_CompanyNo.Text = "";
            add_buyer.Show();
        }
        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Add")
            {
                int companyid = Convert.ToInt32(e.CommandArgument.ToString());
                InspectorChargeCompanyEntity iccEnt = new InspectorChargeCompanyEntity();
                iccEnt.adminId = sysAdminId;
                iccEnt.companyId = companyid;
                logic.inspectorChargeCompany.insert(iccEnt);
                rptBind();
                rpt_icclistBind();
                add_buyer.Show();
                Library.Script.ClientMsg("添加成功.");
                
            }
        }
        protected void btnDel_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < rpt_icclist.Items.Count; i++)
            {
                RepeaterItem row = rpt_icclist.Items[i];
                HiddenField hdcid = row.FindControl("hidiccid") as HiddenField;
                CheckBox chk = row.FindControl("chkChosed") as CheckBox;
                if (chk.Checked)
                {
                    int companyid = Convert.ToInt32(hdcid.Value.ToString());
                    InspectorChargeCompanyEntity iccEnt = new InspectorChargeCompanyEntity();
                    iccEnt.adminId = sysAdminId;
                    iccEnt.companyId = companyid;
                    logic.inspectorChargeCompany.delete(iccEnt);
                }
                
            }
            rpt_icclistBind();
            Library.Script.ClientMsg("删除成功.");
        }
        protected void lbn_resetselect_Click(object sender, EventArgs e)
        {
            rpt_icclistBind();
        }
        protected void lbn_select_Click(object sender, EventArgs e)
        {
            rpt_icclistBind();
        }
        protected void lbn_reset_Click(object sender, EventArgs e)
        {
            rptBind();
            add_buyer.Show();
        }
        protected void btnAddSelected_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < rpt_list.Items.Count; i++)
            {
                RepeaterItem row = rpt_list.Items[i];
                HiddenField hdcid = row.FindControl("hidcompanyid") as HiddenField;
                CheckBox chk = row.FindControl("chkSelected") as CheckBox;
                if (chk.Checked)
                {
                    int companyid = Convert.ToInt32(hdcid.Value.ToString());
                    InspectorChargeCompanyEntity iccEnt = new InspectorChargeCompanyEntity();
                    iccEnt.adminId = sysAdminId;
                    iccEnt.companyId = companyid;
                    logic.inspectorChargeCompany.insert(iccEnt);
                }
            }
            rptBind();
            rpt_icclistBind();
            add_buyer.Show();
            Library.Script.ClientMsg("添加成功.");
        }
    }
}
